
public class ScrollBar extends View implements RectangularButtonDelegate {

	final public static int WIDTH = 15;
	
	final private static int barColor[] = {205, 205, 205};
	final private static int SLIDER_HEIGHT = 7;
	final private static int BTNOFFSET = 5;
	final public static int BTNUP_ID = 0;
	final public static int BTNDOWN_ID = 1;
	
	private int levels;
	private int currentLevel;
	private RectangularButton btnUp;
	private RectangularButton btnDown;
	private ScrollBarDelegate delegate;
	
	/*=============================================================================================
	 * 
	 *-------------------------------------------------------------------------------------------*/
	ScrollBar(float x_, float y_, float h_, MainView p) {
		super(x_, y_, WIDTH, h_, p);
		levels = 1;
		currentLevel = 1;
		delegate = null;
		
		btnUp = new RectangularButton( 0, 0, WIDTH, WIDTH, p);
		btnUp.setId( BTNUP_ID);
		btnUp.setDelegate( this);
		btnUp.disable();

		btnDown = new RectangularButton( 0, h_-WIDTH, WIDTH, WIDTH, p);
		btnDown.setId( BTNDOWN_ID);
		btnDown.setDelegate( this);
		btnDown.disable();
		
		subviews.add(btnUp);
		subviews.add(btnDown);
	}
	
	/*=============================================================================================
	 * 
	 *-------------------------------------------------------------------------------------------*/
	void setDelegate( ScrollBarDelegate pDelegate) { delegate = pDelegate; }
	
	/*=============================================================================================
	 * 
	 *-------------------------------------------------------------------------------------------*/
	int getCurrentLevel() { return currentLevel; }
	
	/*=============================================================================================
	 * 
	 *-------------------------------------------------------------------------------------------*/
	boolean setLevels( int pLevels)
	{
		if( pLevels < 1) return false;
		
		levels = pLevels;
		if( currentLevel>levels)
		{
			currentLevel = levels;
			if( delegate != null) delegate.levelChanged(currentLevel);
		}
		
		if( currentLevel < levels)
			btnDown.enable();
		else
			btnDown.disable();
		
		if( currentLevel > 1)
			btnUp.enable();
		else
			btnUp.disable();
		
		return true;
	}
	
	int getLevels() { return levels; }
	
	/*=============================================================================================
	 * 
	 *-------------------------------------------------------------------------------------------*/
	void drawContent()
	{
		float barHeight = h-(WIDTH+BTNOFFSET)*2;
		float barY = WIDTH+BTNOFFSET;
		
		parent.fill(barColor[0],barColor[1],barColor[2]);
		parent.stroke(255);
		parent.strokeWeight(2);
		parent.rect(0, barY, WIDTH, barHeight);
		if( levels > 1)
		{
			float sliderY;
			if( currentLevel == 1)
				sliderY = barY;
			else if( currentLevel == levels)
				sliderY = barY+barHeight-SLIDER_HEIGHT;
			else
			{
				float levelHeight = barHeight/levels;
				sliderY = barY+(currentLevel-1)*levelHeight+levelHeight/2-SLIDER_HEIGHT/2;
			}
			
			parent.fill(255);
			parent.strokeWeight(2);
			parent.stroke(0);
			parent.rect( 0, sliderY, WIDTH, SLIDER_HEIGHT);
		}
		
		//parent.fill(255);
		//parent.textAlign(PApplet.CENTER, PApplet.CENTER);
		//parent.text(caption, w/2, h/2);
		//*/
	}

	/*=============================================================================================
	 * 
	 *-------------------------------------------------------------------------------------------*/
	public void toBottom()
	{
		if( currentLevel<levels)
		{
			btnUp.enable();
			currentLevel = levels;
			if( delegate != null) delegate.levelChanged(currentLevel);
		}
		
		btnDown.disable();
	}
	
	/*=============================================================================================
	 * 
	 *-------------------------------------------------------------------------------------------*/
	public void RectangularButtonPressed(int id) {
		if( id == BTNUP_ID)
		{
			if( currentLevel > 1)
			{
				currentLevel--;
				if( currentLevel == 1) btnUp.disable();
				btnDown.enable();
				if( delegate != null) delegate.levelChanged(currentLevel);
			}
		}
		else // id == BTNDOWN_ID
		{
			if( currentLevel < levels)
			{
				currentLevel++;
				if( currentLevel == levels) btnDown.disable();
				btnUp.enable();
				if( delegate != null) delegate.levelChanged(currentLevel);
			}
		}
	}

}
